{%- macro lit_field(fl, flabel, multi=False, wrap=None) -%}
{% if fl %}
    {% set sz = fl | length %}
    <section>
        <h2>{{ flabel }}</h2>
        {% if multi and sz > 1 %}
            <ul>
            {% for ds in fl %}
                <li>{{ ds | md | safe }}</li>
            {% endfor %}
            </ul>
        {% else %}
            {% if multi %}{% set fl = fl.pop() %}{% endif %}
            {% if wrap %}
                <{{ wrap }}>
            {% endif %}
            {{ fl | md | safe }}
            {% if wrap %}
                </{{ wrap }}>
            {% endif %}
        {% endif %}
    </section>
{% endif %}
{%- endmacro -%}

{%- macro display_dimensions(dim_fl) -%}
{% if dim_fl %}
    {% set sz = dim_fl | length %}
    <section>
        <h2>Dimensions</h2>
            <ul>
            {% for ds in dim_fl %}
                <li>
                    {{ draw_dimensions(**ds[1]).tostring() | safe }}
                    {{ ds[0] | md | safe }}
                </li>
            {% endfor %}
            </ul>
    </section>
{% endif %}
{%- endmacro -%}

{%- macro uri_field(fl, flabel, multi=True) -%}
{% if fl %}
    {% set sz = fl | length %}
    <section>
        <h2>{{ flabel }}</h2>
        {% if multi and sz > 1 %}
            <ul class="gallery">
            {% for ds in fl %}
                {% set uid, label, icon_uid = link_meta(ds) %}
                <li class="gallery_item"><a href="/view{{ uid }}">
                    {% if icon_uid %}
                        {{ icon_tag(icon_uid, label) }}
                    {% endif %}
                    <span class="icon_label">{{ label | e }}</span>
                </a></li>
            {% endfor %}
            </ul>
        {% else %}
            {% if multi %}{% set fl = fl.pop() %}{% endif %}
            {% set uid, label, icon_uid = link_meta(fl) %}
            <p class="gallery"><a class="gallery_item" href="/view{{ uid }}">
                {% if icon_uid %}
                    {{ icon_tag(icon_uid) }}
                {% endif %}
                <span class="icon_label">{{ label | e }}</span>
            </a></p>
    {% endif %}
    </section>
{% endif %}
{%- endmacro -%}

{%- macro icon_tag(uuid, alt='') -%}
<img class="thumbnail" src="{{ url_for(
          'main.stream_iiif_image',
          path=uuid.lstrip('/') + '/square/128,/0/default.jpg'
          ) }}" alt="{{alt}}"/>
{%- endmacro -%}

